package top.wenzz.manage.system.manage.web;

import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.wenzz.commons.entity.LoginUser;
import top.wenzz.commons.utils.SpringSecurityUtils;
import top.wenzz.commons.vo.RetMsg;
import top.wenzz.commons.web.BasicController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by cui-fate on 2017/7/2.
 */
@Controller
public class IndexController extends BasicController {

    @RequestMapping(value = {"index", "", "login", "403"}, produces = "text/html")
    public String indexHtml(HttpServletRequest request) {
        if(SpringSecurityUtils.getUserByContext() != null) {
            request.setAttribute("loginUser", SpringSecurityUtils.getUserByContext());
        }
        return "index";
    }

    @RequestMapping(value = {"index", "", "login"})
    @ResponseBody
    public Object index(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> retData = getUserData(request);
        if(retData.get("username") == null) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
        return RetMsg.success(retData);
    }
}
